<?php

class TasksController extends ControllerBase {
	private $UserDataService = NULL;
	private $DealOrderDataService = NULL;
	public $grouponCategoryDataService = null;
	private $grouponDataService;
	private $dealsDataService;
	public function indexAction(){
		$this->smarty->assign('pageName', 'tasks');
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function searchAction(){
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/search.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function transferShippingAction() {
		
		$date = date ( 'Y-m-d' );
		$action = $this->getPost ( 'submit', '' );
		
		if ($action != '') {
			$date = $this->getPost ( 'date' );
			// Create new PHPExcel object
			//echo date('H:i:s') . " Create new PHPExcel object\n";
			$objPHPExcel = new PHPExcel();
			// Set properties
			//echo date('H:i:s') . " Set properties\n";
			$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
			$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
			$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
			$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
			$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
			
			
			// Add some data
			$objPHPExcel->setActiveSheetIndex(0);
			$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tên NV');
			$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Deal');
			$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Tên người nhận');
			$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Điện thoại');
			$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Địa chỉ');
			$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Số tiền nhận về');
			
			$dealOrderDataService =  new DealOrderDataService();
			$listItems = $dealOrderDataService->getShippingOfStaff ( array('date' => $date) );
			$i = 2;
			foreach ($listItems as $item) {
				$objPHPExcel->getActiveSheet()->SetCellValue('A'. $i, $item['fullname']);
				$objPHPExcel->getActiveSheet()->SetCellValue('B'. $i, $item['title']);
				$objPHPExcel->getActiveSheet()->SetCellValue('C'. $i, $item['recipient_name']);
				$objPHPExcel->getActiveSheet()->SetCellValue('D'. $i, $item['recipient_phone']);
				$objPHPExcel->getActiveSheet()->SetCellValue('E' .$i, $item['recipient_number'] .' '. $item['recipient_street'].', phường '. $item['recipient_ward_ngo']. ', quận '.$item['recipient_district']);
				$objPHPExcel->getActiveSheet()->SetCellValue('F'. $i, ($item['payment_method'] == 'Invoice')? $item['total_price']:'Không thu');
				$i++;
			}
			// Rename sheet
			$objPHPExcel->getActiveSheet()->setTitle('Simple');
							
			// Save Excel 2007 file
			$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
			$file = date('Y-m') .'-' . CommonUtils::randomNumber(9).'.xlsx';
			$objWriter->save(EXPORTFILE_PATH.'/'. $file);	
//			header('Content-Encoding: UTF-8');
//			header('Content-type: text/csv; charset=UTF-8');
//			header('Content-Disposition: attachment; filename=test.xls');
			
			CommonUtils::redirect ( 'http://'.$_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']).'/Resources/Giaohang/'. $file);
			// Echo done
			//echo date('H:i:s') . " Done writing file.\r\n";
		
		}
		$orderShippingDataService = new OrderShippingDataService ();
		$taskForEachShippinger = $orderShippingDataService->getTotalOrderOfUser ( $date );
		$total = $orderShippingDataService->getTotals ( array ("DATE_FORMAT(shipping_date, '%Y-%m-%d')" => $date ) );
		$this->smarty->assign ( 'total', $total );
		$this->smarty->assign ( 'taskForEachShippinger', $taskForEachShippinger );
		$this->smarty->assign ( 'userEntity', new UserEntity () );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/tranfershipping.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function searchUserAction(){
		$this->UserDataService = new UserDataService();
		$arrElement = array ();
		$arrElementAll = array ();
//		$arrElement ['status'] = UserEntity::ACTIVE;
		$where = '';
		if($this->getPost('submit') == 'Tìm'){
			$fullname = $this->getPost('fullname');
			$email = $this->getPost('email');
			if($fullname != '' && $email =='' ){
				$where .= 'where fullname LIKE "%'.$fullname.'%"';
				
			}
			$this->smarty->assign('fullname', $fullname);
			
			if($email != '' && $fullname == ''){
				$where .= 'where email LIKE "%'.$email.'%"';
			}
			$this->smarty->assign('email', $email);
			if ($fullname!='' && $email!=''){
				$where .= ' where fullname LIKE "%'.$fullname.'%" AND email LIKE "%'.$email.'%"';
			}
			$this->smarty->assign('fullname',$fullname);
			$this->smarty->assign('email', $email);
		}
		$numRecordPerPage = ITEM_PER_PAGE;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->UserDataService->getTotals ( $arrElement );	
		$itemList = $this->UserDataService->getItemsAll( $arrElement,null, $start, $numRecordPerPage, $where );		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Tasks/searchUser";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/searchUser.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function searchOrderAction(){
		$this->DealOrderDataService = new DealOrderDataService();
		$arrElement = array ();
		$arrElementAll = array ();
//		$arrElement ['status'] = UserEntity::ACTIVE;
		$where = '';
		if($this->getPost('submit') == 'Tìm'){
			$email = $this->getPost('email');
			$phone = $this->getPost('phone');
			$fromDate = $this->getPost('txtFrom');
			$untilDate = $this->getPost('txtUntil');
			if($email != '' ){
				$where .= 'AND U.email LIKE "%'.$email.'%"';
				
			}
			$this->smarty->assign('email', $email);
			if($phone != '' ){
				$where .= 'AND U.phone LIKE "%'.$phone.'%"';
				
			}
			$this->smarty->assign('phone', $phone);
			if($fromDate !=''){
				$where .= "AND DATE_FORMAT(OD.order_date, '%Y-%m-%d %H:%i:%s') >= '".date('Y-m-d', strtotime($fromDate)) ."'";
			}
			$this->smarty->assign('txtFrom', $fromDate);
			if($untilDate !=''){
				 $where .= "AND DATE_FORMAT(OD.order_date, '%Y-%m-%d %H:%i:%s') <= '".date('Y-m-d', strtotime($untilDate)) ."'";
			}
			$this->smarty->assign('txtUntil', $untilDate);
		}
		$numRecordPerPage = ITEM_PER_PAGE;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->DealOrderDataService->getTotals($arrElement);	
		$itemList = $this->DealOrderDataService->getAllItems( $arrElement,null, $start, $numRecordPerPage, $where );		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Tasks/searchOrder";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/searchOrder.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	function searchDealAction(){
		$this->dealsDataService = new DealsDataService();
		$this->grouponCategoryDataService = new GrouponCategoryDataService();
		$arrElement = array ();
		$arrElementAll = array ();
		$status = -1;
		if($this->getQuery('status') != ''){
			$arrElement ['status'] = (int)$this->getQuery ( 'status' , 0);
			$arrElementAll['D.status'] = (int)$this->getQuery ( 'status', 0);
			$status = (int)$this->getQuery ( 'status', 0);
		}
		
		$where = '';
		
		if($this->getPost('submit') == 'Tìm'){
			if($this->getPost('groupon_name') != ''){
				$where .= ' AND G.groupon_name LIKE "%'.$this->getPost('groupon_name').'%"';				
			}
			$this->smarty->assign('groupon_name', $this->getPost('groupon_name'));
			if($this->getPost('started_deal') != ''){
				$where .=' AND D.started_deal > '. $this->getPost('started_deal');					
			}
			$this->smarty->assign('started_deal', $this->getPost('started_deal'));	
			if($this->getPost('expired_deal') != ''){
				$where .=' AND D.expired_deal > '. $this->getPost('expired_deal');
			}
			$this->smarty->assign('expired_deal', $this->getPost('expired_deal'));
			if($this->getPost('category') != ''){
				$where .=' AND GC.id = '. $this->getPost('category');				
			}
			$this->smarty->assign('category', $this->getPost('category'));	
			if($this->getPost('deal_status') != '-1'){
				$arrElement['status'] = (int)$this->getPost('deal_status');
				$where .=' AND D.status = '. $this->getPost('deal_status');	
				$status = $this->getPost('deal_status');					
			}

		}
		$this->smarty->assign('deal_status', $status);
		$numRecordPerPage = ITEM_PER_PAGE;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->dealsDataService->getTotals( $arrElement );
		
		$itemList = $this->dealsDataService->getItemsAll ( $arrElement, 'D.id desc', $start, $numRecordPerPage, $where );
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Tasks/searchDeal";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'dealEntity', new DealsEntity () );
		$this->smarty->assign('dealStatusList', DealsEntity::getStatusList());
		$this->smarty->assign('categoryList', $this->grouponCategoryDataService->getItems(array()));
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Tasks/searchDeal.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
}
